/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package chatsystem;

import chatsystem.Controller;
import chatsystem.ListeUtilisateurs;
import chatsystem.Utilisateur;
import java.awt.Component;
import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Observable;
import java.util.Observer;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.MouseInputListener;
import javax.swing.plaf.basic.BasicComboBoxUI.ListDataHandler;

/**
 * @deprecated 
 * @author morrison
 */
public class ConversationPanel extends JPanel implements Observer{

    private Controller control;
    private ListeUtilisateurs usersList;
    private JLabel labelChatters;
    private JList listeChatters;
    private JTabbedPane conversation;
    private JButton disconnect;
    private JTextField message;
    private JButton send;
    private JSeparator separateur;

    private void btnDisconnect() {
        this.control.disconnect();
    }

    private void btnSend() {
        this.control.sendMessage(conversation.getTitleAt(conversation.getSelectedIndex()), message.getText());
        this.message.setText("");
    }

    //TODO à checker
    private void setTab(int indexFromList) {
        String username = ((Utilisateur) usersList.getElementAt(indexFromList)).getUserName();
        int tabIndex = this.getTabIndex(username);
        this.conversation.setEnabledAt(tabIndex, true);
    }

    /**
     * 
     * @param user
     * @return 
     */
    private JTextArea getTab(String user) {
        boolean found = false;
        int i = 0;
        int last = this.conversation.getTabCount() - 1;

        if (last >= 0) {
            Component tab = this.conversation.getComponentAt(i);
            while (i < last && !tab.getName().equals(user)) {

                i++;
                tab = this.conversation.getComponentAt(i);
            }

            if (tab.getName().equals(user)) {
                found = true;
            }
        }

        if (!found) { // tab doesn't exist yet
            this.conversation.addTab(user, new JTextArea());
            return (JTextArea) this.conversation.getComponentAt(this.conversation.getTabCount() - 1);
        } else {
            return (JTextArea) this.conversation.getComponentAt(i);
        }
    }

    /**
     * si l'utilisateur a déjà un onglet, on retourne son index
     * sinon on crée un nouvel onglet et on retourne son index
     * @param user
     * @return
     */
    private int getTabIndex(String user) {
        int i = 0;
        int last = this.conversation.getTabCount() - 1;

        while (i <= last && !this.conversation.getTitleAt(i).equals(user)) {
            i++;
        }

        if (i > last) {
            this.conversation.addTab(user, new JTextArea());
        }
        //TODO verifier retour si tab not found
        return i;
    }

    public void addStringToConversation(String user, String mess) {
        JTextArea tab = (JTextArea) this.conversation.getComponentAt(getTabIndex(user));
        tab.append(mess+"\n");
    }
    
    public void displayErrorNotification(String user, String notif) {
        JTextArea tab = (JTextArea) this.conversation.getComponentAt(getTabIndex(user));
        tab.append("Error : "+notif+"\n");
    }


    public ConversationPanel(Controller control, ListeUtilisateurs list) {

        super();
        this.control = control;
        this.usersList = list;

        this.setLayout(new GridBagLayout());
        GridBagConstraints c;

        c = new GridBagConstraints();
        labelChatters = new JLabel("Ready to chat");
        c.gridx = 0;
        c.gridy = 0;
        this.add(labelChatters, c);


        //A - Liste cliquable des chatters
        c = new GridBagConstraints();
        listeChatters = new JList(this.usersList);
        //TODO à checker
        listeChatters.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 2) {
                    int index = listeChatters.locationToIndex(e.getPoint());
                    setTab(index);
                    System.out.println("Double clicked on Item " + index);
                }
            }
        });

        c.gridx = 0;
        c.gridy = 1;
        c.fill = GridBagConstraints.BOTH;


        this.add(listeChatters, c);
        //C - JTextField Conversation
        c = new GridBagConstraints();
        conversation = new JTabbedPane();
        c.fill = GridBagConstraints.BOTH;
        c.gridx = 2;
        c.gridy = 0;
        c.gridwidth = 2;
        c.gridheight = 2;
        c.weighty = 1.0;


        this.add(conversation, c);
        //D - JButton Disconnect
        c = new GridBagConstraints();
        disconnect = new JButton("Disconnect");

        disconnect.addActionListener(
                new ActionListener() {

                    public void actionPerformed(ActionEvent ae) {
                        btnDisconnect();
                    }
                });
        c.gridx = 0;
        c.gridy = 2;
        this.add(disconnect, c);

        //E - JTextField pour écrire le mess
        c = new GridBagConstraints();
        message = new JTextField("", 2);
        c.gridx = 2;
        c.gridy = 2;
        c.weightx = 1.0;
        //c.weighty=1.0;
        c.fill = GridBagConstraints.HORIZONTAL;
        this.add(message, c);

        //F - JButton Envoyer
        c = new GridBagConstraints();
        send = new JButton("Send");
        send.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ae) {
                btnSend();
            }
        });
        c.gridx = 3;
        c.gridy = 2;
        this.add(send, c);

        c = new GridBagConstraints();
        separateur = new JSeparator(JSeparator.VERTICAL);
        c.gridx = 1;
        this.add(separateur, c);

        this.setPreferredSize(new Dimension(600, 400));
    }

    public void update(Observable o, Object o1) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
